<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="标准名称" prop="pollingName">
        <el-input v-model="queryParams.pollingName" size="small" placeholder="请输入标准名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="关联设备" prop="createBy">
        <el-input v-model="queryParams.createBy" size="small" placeholder="请输入关联设备" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="所属部门" prop="deptId">
        <treeselect style="width: 200px;" v-model="queryParams.deptId" :options="deptOptions" :show-count="true" :clearable="false" 
          placeholder="请选择归属部门" :normalizer="normalizer" />

      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['polling:standard:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          :loading="exportLoading" v-hasPermi="['polling:standard:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table stripe v-loading="loading" :data="list" :default-sort="{prop: 'id', order: 'descending'}">
      <el-table-column label="序号" align="center" sortable prop="id" />
      <el-table-column label="标准编号" align="center" sortable prop="pollingCode">
        <template slot-scope="scope">            <el-popover trigger="hover" placement="top">              <p>标准编号: {{ scope.row.pollingCode }}</p>              <p>标准名称: {{ scope.row.pollingName }}</p>             <div slot="reference" class="name-wrapper" @click="hxySkipPage(scope.row.id)">                <el-tag size="small">{{ scope.row.pollingCode }}</el-tag>              </div>           </el-popover>          </template>
      </el-table-column>
      <el-table-column label="标准名称" align="center" sortable prop="pollingName" :show-overflow-tooltip='true'>
        <template slot-scope="scope">           <el-popover trigger="hover" placement="top">             {{ scope.row.pollingName }}           </el-popover>           {{ scope.row.pollingName }}          </template>
      </el-table-column>
      <el-table-column label="所属部门" align="center" sortable prop="deptName" />
      <el-table-column label="适用类别" align="center" sortable prop="addDemand" />
      <el-table-column label="要求" align="center" sortable prop="addDemand" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" sortable prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 时间问题  先关闭 删除修改都需要加入判断   -->
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['polling:standard:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['polling:standard:delete']">删除</el-button> -->
            --
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <fieldset style="margin-top: -22px">
          <legend>基础信息</legend>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="标准名称" prop="pollingName">
                <el-input v-model="form.pollingName" placeholder="请输入标准名称" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="所属部门" prop="deptId">
                <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" :clearable="false"
                  placeholder="请选择归属部门" :normalizer="normalizer" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="适用类别" prop="typeId">
                <el-select v-model="form.typeId" placeholder="请输入适用类别" style="width: 100%;">                    <el-option v-for="item in typesList" :key="item.id" :label="item.typeName" :value="item.id">
                  </el-option>                 </el-select>

              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="要求" prop="addDemand">
                <el-input v-model="form.addDemand" placeholder="要求" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>

          </el-row>
        </fieldset>

        <fieldset>
          <legend>设备信息</legend>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="设备类型" prop="communityId">
                <el-select v-model="form.imeiType" placeholder="请选择设备类型" @change="getBuildingData" style="width: 100%;"
                  :disabled="false">
                  <el-option v-for="dict in imeiTypesList" :key="dict.id" :label="dict.emqName" :value="dict.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="设备" prop="imei">
                <el-select v-model="form.imei" placeholder="请选择设备" style="width: 100%;" :multiple="true"
                  :disabled="false">
                  <el-option v-for="dict in imeiList" :key="dict.id" :label="dict.emqName" :value="dict.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

        </fieldset>

        <fieldset>
          <legend>项目信息</legend>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="项目" prop="communityId">
                <el-select v-model="form.items" placeholder="请选择项目" :multiple="true" :disabled="false">
                  <el-option v-for="dict in itemsList" :key="dict.id" :label="getTextLable(dict) " :value="dict.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

        </fieldset>




      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    createStandard,
    updateStandard,
    deleteStandard,
    getStandard,
    getStandardPage,
    exportStandardExcel,
    getCategoriesList,
    getImeiTypeList,
    getImeiInfo,
    getStandardMy
  } from "@/api/polling/standard";

  import {
    getPollingProjectPage
  } from "@/api/polling/pollingProject"

  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import Treeselect from "@riophae/vue-treeselect";
  import {
    listSimpleDepts
  } from "@/api/system/dept";
  export default {
    name: "Standard",
    components: {
      Treeselect
    },
    data() {
      return {
        // 部门树选项
        deptOptions: undefined,
        communityOptions: [],
        typesList: [],
        imeiTypesList: [],
        imeiList: [],
        itemsList: [],
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 巡检标准列表
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          createBy: null,
          createTime: [],
          updateBy: null,
          pollingCode: null,
          pollingName: null,
          deptId: null,
          addDemand: null,
          remark: null,
        },
        optionsTypeList: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          pollingCode: [{
            required: true,
            message: "标准编号不能为空",
            trigger: "blur"
          }],
          pollingName: [{
            required: true,
            message: "标准名称不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getTreeselect();
      this.initData();
    },
    methods: {

      getTextLable(dict) {
        return dict.ppName + "[" + dict.ppCode + "]";
      },
      initData() {
        var querys = {
          pageNo: 1,
          pageSize: 50
        };
        getCategoriesList(querys).then(res => {

          this.typesList = res.data.list;         });          getImeiTypeList().then(res => {
          this.imeiTypesList = res.data;          });

        getPollingProjectPage(querys).then(res => {
          this.itemsList = res.data.list          });
      },
      hxySkipPage(item) {
        this.$router.push({
          path: '/polling/info',
          query: {
            uid: item
          }
        })
      },
      getBuildingData(a) {
        getImeiInfo(a).then(res => {
          console.log("select", res)
          this.imeiList = res.data;
        });

      },
      getTreeselect() {
        listSimpleDepts().then(response => {
          // 处理 deptOptions 参数
          this.deptOptions = [];
          this.deptOptions.push(...this.handleTree(response.data, "id"));
        });
      },
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },

      /** 查询列表 */
      getList() {
        this.loading = true;
        // 执行查询
        //getStandardPage(this.queryParams).then(response => {
          getStandardMy(this.queryParams).then(response => {
          console.log("response", response)
          this.list = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      /** 取消按钮 */
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 表单重置 */
      reset() {
        this.form = {
          createBy: undefined,
          updateBy: undefined,
          pollingId: undefined,
          pollingCode: undefined,
          pollingName: undefined,
          deptId: undefined,
          addDemand: undefined,
          remark: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNo = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加巡检标准";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const pollingId = row.pollingId;
        getStandard(pollingId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改巡检标准";
        });
      },
      /** 提交按钮 */
      submitForm() {

        console.log("this.form", this.form)
        return;
        this.form.pollingCode = "-1";
        this.$refs["form"].validate(valid => {
          if (!valid) {
            return;
          }
          // 修改的提交
          if (this.form.pollingId != null) {
            updateStandard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            return;
          }
          // 添加的提交
          createStandard(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const pollingId = row.pollingId;
        this.$modal.confirm('是否确认删除巡检标准编号为"' + pollingId + '"的数据项?').then(function() {
          return deleteStandard(pollingId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        // 处理查询参数
        let params = {
          ...this.queryParams
        };
        params.pageNo = undefined;
        params.pageSize = undefined;
        this.$modal.confirm('是否确认导出所有巡检标准数据项?').then(() => {
          this.exportLoading = true;
          return exportStandardExcel(params);
        }).then(response => {
          this.$download.excel(response, '巡检标准.xls');
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
